package com.bh.backGoods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.bh.common.annotation.DataDictClass;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.RedisCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.bh.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bh.backGoods.domain.JcBackGoodsItem;
import com.bh.backGoods.mapper.JcBackGoodsMapper;
import com.bh.backGoods.domain.JcBackGoods;
import com.bh.backGoods.service.IJcBackGoodsService;

/**
 * 内部返货Service业务层处理
 * 
 * @author slx
 * @date 2024-04-03
 */
@Service
public class JcBackGoodsServiceImpl extends ServiceImpl<JcBackGoodsMapper, JcBackGoods> implements IJcBackGoodsService
{
    @Autowired
    private JcBackGoodsMapper jcBackGoodsMapper;
    @Autowired
    private RedisCodeUtil redisCodeUtil;

    /**
     * 查询内部返货
     * 
     * @param id 内部返货主键
     * @return 内部返货
     */
    @Override
    public JcBackGoods selectJcBackGoodsById(Long id)
    {
        return jcBackGoodsMapper.selectJcBackGoodsById(id);
    }

    /**
     * 查询内部返货列表
     * 
     * @param jcBackGoods 内部返货
     * @return 内部返货
     */
    @DataDictClass
    @Override
    public List<JcBackGoods> selectJcBackGoodsList(JcBackGoods jcBackGoods)
    {
        return jcBackGoodsMapper.selectJcBackGoodsList(jcBackGoods);
    }

    /**
     * 新增内部返货
     * 
     * @param jcBackGoods 内部返货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJcBackGoods(JcBackGoods jcBackGoods)
    {
        jcBackGoods.setLicense(jcBackGoods.getLicense().toUpperCase());
        jcBackGoods.setCode(redisCodeUtil.getBackCode());
        jcBackGoods.setCreateTime(DateUtils.getNowDate());
        int rows = jcBackGoodsMapper.insert(jcBackGoods);
        insertJcBackGoodsItem(jcBackGoods);
        return rows;
    }

    /**
     * 修改内部返货
     * 
     * @param jcBackGoods 内部返货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJcBackGoods(JcBackGoods jcBackGoods)
    {
        jcBackGoods.setModifyTime(DateUtils.getNowDate());
        jcBackGoods.setModifyName(jcBackGoods.getUpdateBy());
        jcBackGoods.setLicense(jcBackGoods.getLicense().toUpperCase());
        jcBackGoodsMapper.deleteJcBackGoodsItemByBackId(jcBackGoods.getId());
        insertJcBackGoodsItem(jcBackGoods);
        return jcBackGoodsMapper.updateById(jcBackGoods);
    }

    @Override
    public int updateJcBackGoodsStatus(JcBackGoods jcBackGoods) {
        JcBackGoods old = jcBackGoodsMapper.selectJcBackGoodsById(jcBackGoods.getId());
        if (StringUtils.isNotEmpty(jcBackGoods.getSendImgs()) && old.getStatus().equals("created")) {
            jcBackGoods.setStatus("sended");
            jcBackGoods.setSendDate(DateUtils.getNowDate());
        } else if (StringUtils.isNotEmpty(jcBackGoods.getReceiveImgs()) && old.getStatus().equals("sended")) {
            jcBackGoods.setStatus("received");
            jcBackGoods.setReceiveDate(DateUtils.getNowDate());
        }
        return jcBackGoodsMapper.updateById(jcBackGoods);
    }

    /**
     * 批量删除内部返货
     * 
     * @param ids 需要删除的内部返货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJcBackGoodsByIds(Long[] ids)
    {
        jcBackGoodsMapper.deleteJcBackGoodsItemByBackIds(ids);
        return jcBackGoodsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除内部返货信息
     * 
     * @param id 内部返货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJcBackGoodsById(Long id)
    {
        jcBackGoodsMapper.deleteJcBackGoodsItemByBackId(id);
        return jcBackGoodsMapper.deleteById(id);
    }

    /**
     * 新增返货详情信息
     * 
     * @param jcBackGoods 内部返货对象
     */
    public void insertJcBackGoodsItem(JcBackGoods jcBackGoods)
    {
        List<JcBackGoodsItem> jcBackGoodsItemList = jcBackGoods.getJcBackGoodsItemList();
        Long id = jcBackGoods.getId();
        if (StringUtils.isNotNull(jcBackGoodsItemList))
        {
            List<JcBackGoodsItem> list = new ArrayList<JcBackGoodsItem>();
            for (JcBackGoodsItem jcBackGoodsItem : jcBackGoodsItemList)
            {
                jcBackGoodsItem.setBackId(id);
                list.add(jcBackGoodsItem);
            }
            if (list.size() > 0)
            {
                jcBackGoodsMapper.batchJcBackGoodsItem(list);
            }
        }
    }
}
